import os
#import mxnet as mx
import logging
import numpy as np
import json
from flask import jsonify
#from skimage import io, transform
from flask import Flask, request, redirect, url_for, render_template
from flask import send_from_directory
from predict import predict
#from werkzeug import secure_filename
app = Flask(__name__)



@app.route('/')
def begin():
    return render_template('milGo.html')

@app.route('/dataconvector')
def dataConvector():

    mydata = json.loads(request.args.get('mykey'))
    print mydata
    state = np.zeros((19,19))

    for i in range(19):
        for j in range(19):
            state[i][j] = mydata['msg'][i][j]

    print state
    pos = predict(state)
    positions = []
    state = state.reshape(361,1)
    for i in range(len(state)):
        # print len(state[0])
        # print state[0][i],i
        if state[i] != 0:
            positions.append(i)
    i = 0
    print pos[i]
    print positions
    while pos[i]  in positions:
        # positions.append(pos[i])
        # mydata['msg'] = pos[i]
        # print mydata['msg']
        i+=1
    mydata['msg'] = pos[i]

    return jsonify(result = mydata['msg'])

    # if pos[0] is not in positions:
    #     positions.append(pos[0])
    #     mydata['msg'] = pos[0]
    #     print mydata['msg']
    #     return jsonify(result = mydata['msg'])
    # else:
    #     i = 0
    #     while pos[i] is not in positions:


    # print len(mydata['msg']),type(mydata['msg'][0])
    # print mydata[u'msg']
